/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.productionPlanning.managedbean;

import java.io.IOException;
import java.io.InputStream;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author GKnight
 */
@ManagedBean
@ViewScoped
public class FileManagedBean {
    private StreamedContent file;

    public FileManagedBean(){
    }
    
    public StreamedContent getFile() throws IOException {
        InputStream stream = ((HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true)).getServletContext().getResourceAsStream("/jasperreports/forecastResult.pdf");
        
        if (stream == null){
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "The report has not been generated yet", ""));
        }
        file = new DefaultStreamedContent(stream, "application/pdf","forecastResult.pdf");
        return this.file;
    }
    
}
